package com.solveit.aps.io.db;

import com.solveit.core.db2.generator.Manager;
import com.solveit.core.db2.generator.MetaClass;
import com.solveit.core.db2.generator.ObjectGenerator;
import com.solveit.core.db2.generator.PrimaryKey;

/**
 * Call this component as early as possible as it initialises the loading of
 * static entities which have the same object reference throughout the runtime
 * of the application.
 * 
 * @author sschellenberg
 * 
 */
public class DataBaseComponent {

	public DataBaseComponent() {
		// TODO: force the classLoader to load these classes prior to loading any data from the db...
//		System.out.println(APSResourceAreas.City);
//		System.out.println(ResourceType.Berth);
//		System.out.println(ActivityType.ArriveLoadStation);
//		System.out.println(RPOStatus.ASSIGNED);
		
		InternalDbEntityHelper.create();
		Manager.init(new ObjectGenerator() {
			@Override
			public Object resolveRelation(Object target, Class<?> type, MetaClass meta, PrimaryKey foreignKey) {
				return StaticEntityContainer.getInstance().retrieve(target, type, meta, foreignKey);
			}

			@Override
			public Object recordGenerated(Object record) {
				return StaticEntityContainer.getInstance().store(record);
			}
		});
		StaticEntityContainer.create();
//		StaticEntityContainer.getInstance().loadStatics();

		// TODO: remove when finished...
//		System.out.println("Just for testing...");
//		for (ResourceArea resourceArea : Network.getInstance().getResourceAreas()) {
//			Set<ResourceArea> destinations = new HashSet<ResourceArea>();
//			resourceArea.collectDestinations(destinations);
//			System.out.println(resourceArea + " includes, is part of or can reach: " + System.identityHashCode(resourceArea));
//			System.out.println("    resources=" + resourceArea.getResources());
//			destinations.remove(resourceArea);
//			List<ResourceArea> list = new ArrayList<ResourceArea>(destinations);
//			Collections.sort(list, new Comparator<ResourceArea>() {
//				@Override
//				public int compare(ResourceArea o1, ResourceArea o2) {
//					return o1.getName().compareTo(o2.getName());
//				}
//			});
//			System.out.println("     => " + destinations);
//		}
	}
}
